﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ParkingLot.Utility.DongleAndDecip
{
    public class AES256Provider
    {
        private AES256Provider()
        {
        }


        /// <summary>
                /// AES加密
                /// </summary>
                /// <param name="encryptStr">明文</param>
                /// <param name="key">密钥</param>

        /// 16位密钥对应128位编码，24位密钥对应192位，32对应256位编码
                /// <returns></returns>
        public static string Encrypt(string encryptStr, string key)

        {
            try
            {
                byte[] keyArray = hexStr2ByteArray(key);
                byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;//编码方式
                rDel.Padding = PaddingMode.PKCS7;//填充方式
                ICryptoTransform cTransform = rDel.CreateEncryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return byteArray2HexStr(resultArray);
            }
            catch (Exception ex)
            {

            }
            return null;
        }
        /// <summary>
                /// AES解密
                /// </summary>
                /// <param name="decryptStr">密文</param>
                /// <param name="key">密钥</param>
                /// <returns></returns>
        public static string Decrypt(string decryptStr, string key)

        {
            try
            {
                byte[] keyArray = hexStr2ByteArray(key);
                byte[] toEncryptArray = hexStr2ByteArray(decryptStr);
                RijndaelManaged rDel = new RijndaelManaged();
                rDel.Key = keyArray;
                rDel.Mode = CipherMode.ECB;
                rDel.Padding = PaddingMode.PKCS7;
                ICryptoTransform cTransform = rDel.CreateDecryptor();
                byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return UTF8Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception ex)
            {

            }
            return null;
        }
        public static byte[] hexStr2ByteArray(String hexString)
        {
            try
            {
                if (string.IsNullOrEmpty(hexString))
                    throw new Exception("this hexString must not be empty");

                hexString = hexString.ToLower();
                byte[] byteArray = new byte[hexString.Length / 2];
                int k = 0;
                for (int i = 0; i < byteArray.Length; i++)
                {

                    //把hexString中的两个16进制数转化为字节，一个16进制数是4位，两个就是8位即一个字节
                    //Substring(k, 2)的作用是从k开始截取2个数据
                    byteArray[i] = Convert.ToByte(hexString.Substring(k, 2), 16);
                    k += 2;
                }
                return byteArray;
            }
            catch (Exception ex)
            {

            }
            return null;
        }
        public static String byteArray2HexStr(byte[] byteArray)
        {
            try
            {
                StringBuilder hexString = new StringBuilder();
                foreach (byte b in byteArray)
                {

                    //X2表示十六进制格式（大写），域宽2位，不足的左边填0。
                    hexString.AppendFormat("{0:x2}", b);
                }
                return hexString.ToString().ToLower();
            }
            catch (Exception ex)
            {

            }
            return null;
        }
    }
}
